<?php
namespace app\operator\controller;
use think\Db;
use think\Validate;

class Index extends Common
{
	//附近商家
    public function index()
    {
    	
    }
    //获取首页信息
    public function getinfo(){
        $oid = input('param.oid');
        $operator['todayrecharge'] = Db::name('rechargeorder')->where(['oid'=>$oid,'pay_status'=>1])->whereTime('create_time', 'today')->sum('balance');
        $operator['lastdayrecharge'] = Db::name('rechargeorder')->where(['oid'=>$oid,'pay_status'=>1])->whereTime('create_time', 'yesterday')->sum('balance');
        $operator['todayordernum'] = Db::name('leaseorder')->where('oid',$oid)->whereTime('create_time', 'today')->count();
        $operator['lastdayordernum'] = Db::name('leaseorder')->where('oid',$oid)->whereTime('create_time', 'yesterday')->count();
        $operator['userbalance'] = Db::name('user')->where(['oid'=>$oid,'is_delete'=>1])->sum('balance');
        $operator['userdeposit'] = Db::name('user')->where(['oid'=>$oid,'is_delete'=>1])->sum('deposit');

        $info['code'] = 1;
        $info['operator'] = $operator;
        return $info;
    }
    //绑定运营商
    public function bindoperator()
    {
    	$data = input('param.');

        $operator = $data['operator'];
        $validate = \think\Loader::validate('Operator');   //引入验证类
        //输入验证，验证不通过直接返回
        if(!$validate->scene('add')->check($operator)){
            $res['code'] = 0;
            $res['msg'] =  $validate->getError();
            return $res;
        }

        $oid = Db::name('operator')->where('identifying',$operator['identifying'])->field('id')->find();
        if(!$oid){
            $info['code'] = 0;
            $info['msg'] = '企业标识不存在！';
            return $info;
        }

        $user = Db::name('operator_users')->where('oid',$oid['id'])->where(['username' =>$operator['name'],'deleted'=>0])->field('id,openid,username,password,status')->find();

        if(!$user){
            $info['code'] = 0;
            $info['msg'] = '用户信息错误！';
            return $info;
        } elseif($user['password'] != md123($operator['password'])){
            $info['code'] = 0;
            $info['msg'] = '用户名或密码错误！';
            return $info;
        } elseif ($user['openid']&&!$operator['confirm']) {
            $info['code'] = 2;
            $info['msg'] = '此账号已经绑定了其他微信！';
            return $info;
        } elseif ( 0 == $user['status']) {
            $info['code'] = 0;
            $info['msg'] = '帐号被禁用！';
            return $info;
        }elseif ( 2 == $user['status']) {
            $info['code'] = 0;
            $info['msg'] = '帐号审核中';
            return $info;
        } else {
            $openid = mwdecrypt($data['openid']);
            Db::name('operator_users')
            ->where('id',$user['id'])
            ->update(['openid' => $openid]);

            return ['code'=>1,'oid'=>$oid['id']];

        }
    }
  
}
